package com.rymcu.subject.service.impl;

import com.rymcu.subject.dto.SubjectQuestionDTO;
import com.rymcu.subject.entity.Question;
import com.rymcu.subject.entity.QuestionBase;
import com.rymcu.subject.entity.SubjectQuestion;
import com.rymcu.subject.mapper.SubjectQuestionMapper;
import com.rymcu.subject.service.SubjectQuestionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class SubjectQuestionServiceImpl implements SubjectQuestionService {

    @Resource
    private SubjectQuestionMapper subjectQuestionMapper;


    @Override
    public long insertSelective(SubjectQuestion record) {
        return subjectQuestionMapper.insertSelective(record) == 0 ? -1 : record.getId();
    }


    @Override
    public int updateByPrimaryKeySelective(SubjectQuestion record) {
        return subjectQuestionMapper.updateByPrimaryKeySelective(record);
    }


    /**
     * get next subject_question
     *
     * @param userId user id
     * @return next subject_question
     */
    @Override
    public SubjectQuestionDTO getNextByUserId(Long userId) {
        // 题库数量不算太大，这里直接强转int
        final int count = (int)this.subjectQuestionMapper.getAppropriateCount();
        Random random = new Random();
        final long sqId = this.getSqId(userId, random, count);
        return this.subjectQuestionMapper.selectByPrimaryKey(sqId);
    }

    /**
     * 查看试题
     *
     * @param sqId
     * @return
     */
    @Override
    public Question selectBySqId(long sqId) {
        return this.subjectQuestionMapper.selectBySqId(sqId);
    }

    @Override
    public int countList(QuestionBase questionBase) {
        return this.subjectQuestionMapper.countList(questionBase);
    }

    @Override
    public List<Question> listBySqId(List<Integer> sqIdList) {
        return this.subjectQuestionMapper.listBySqId(sqIdList);
    }

    @Override
    public List<Integer> sqIdList(QuestionBase questionBase) {
        return this.subjectQuestionMapper.sqIdList(questionBase);
    }

    @Override
    public List<SubjectQuestionDTO> getAnswerRecord(long userId, Date now, boolean everydayFlag) {
        return this.subjectQuestionMapper.getAnswerRecord(userId, now, everydayFlag);
    }

    /**
     * 获取每日一题的题id
     *
     * @param userId
     * @param random
     * @param count
     * @return
     */
    private long getSqId(long userId, Random random, int count) {
        // random.nextLong() % count 出现了负数，导致服务异常，这里直接使用random.nextInt(int bound)
        final int nextOrder = random.nextInt(count) ;
        final long nextSqId = this.getNextSqId(nextOrder);
        final boolean isRepeat = this.subjectQuestionMapper.isRepeatBySqIdAndUserId(nextSqId, userId);
        if (isRepeat) {
            getSqId(userId, random, count);
        }
        return nextSqId;
    }

    /**
     * get next sqId
     * <p>
     * 注意，mysql函数limit下标是从0开始，即取第一条为"limit 0,1"
     *
     * @return sqId
     */
    private long getNextSqId(long nextOrder) {
        final long nextSqId = this.subjectQuestionMapper.getSqIdByNextOrder(nextOrder);
        return nextSqId;
    }

    /**
     * get next subject_question
     *
     * @param id user 题库id
     */
    @Override
    public SubjectQuestionDTO getById(Long id) {
        return this.subjectQuestionMapper.selectByPrimaryKey(id);
    }

    /**
     * get next subject_question
     *
     * @param userId user id
     * @return next subject_question
     */
    @Override
    public long getNextIdByUserId(Long userId) {
        // 题库数量不算太大，这里直接强转int
        final int count = (int)this.subjectQuestionMapper.getAppropriateCount();
        Random random = new Random();
        final long sqId = this.getSqId(userId, random, count);
        return sqId;
    }
}
